package action;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import entity.Admin;
import entity.Score;
import entity.Student;
import entity.Subject;
import entity.Teacher;
import service.AdminService;
import service.ClassService;
import service.ScoreService;
import service.StudentService;
import service.SubjectService;
import service.TeacherService;

@Controller
@RequestMapping("/admin")
public class AdminAction {

	@Autowired
	private AdminService adminService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private ClassService classService;
	@Autowired
	private ScoreService scoreService;
	@Autowired
	private SubjectService subjectService;
	@RequestMapping("loginView.do")
	protected String loginView() {
		return "/admin/login";
		
	}
	@RequestMapping("login.do")
	protected String login(Admin admin,Model model,HttpSession session) {
		if(adminService.login(admin)) {
			session.setAttribute("admin", admin);
			return "/admin/index";
		}else {	
			model.addAttribute("info","账号或密码错误");
			return "/admin/login";
		}
	}
	@RequestMapping("teacher/list.do")
	protected String teacherList(HttpSession session,Model model,String info) {
		model.addAttribute("teachers",teacherService.findAllTeacher(session));
		model.addAttribute("info", info);
		return "/admin/teacher_list";
	}
	@RequestMapping("teacher/addView.do")
	protected String teacherAddView() {
		return "/admin/teacher_add";
	}
	@RequestMapping("teacher/add.do")
	protected String teacherAdd(Teacher teacher,Model model,HttpSession session) {
		System.out.println(teacher.toString());
		String info = teacherService.add(teacher,session);
		model.addAttribute("info", info);
		if(info.equals("添加成功")) {
			return "redirect:/admin/teacher/list.do";
			
		}else {
			return "/admin/teacher_add";
		}
	}
	@RequestMapping("teacher/delete.do")
	protected String teacherDelete(Teacher teacher,Model model,HttpSession session) {
		String info = teacherService.delete(teacher, session);
		model.addAttribute("info", info);
		if(info.equals("删除成功")) {
			return "redirect:/admin/teacher/list.do";
		}else {
			return "forward:/admin/teacher/list.do";
		}
		
	}
	@RequestMapping("teacher/alterView.do")
	protected String teacherAlterView(String id,Model model) {
		model.addAttribute("teacher",teacherService.findById(id));
		return "/admin/teacher_alter";
	}
	@RequestMapping("teacher/alter.do")
	protected String teacherAlter(Teacher teacher,Model model ,HttpSession session) {
		String info = teacherService.alter(teacher, session);
		model.addAttribute("info",info);
		if(info.equals("修改成功")) {
			return "redirect:/admin/teacher/list.do";
		}else {
			model.addAttribute("teacher", teacher);
			return "/admin/teacher_alter";
		}
	}
	@RequestMapping("student/list.do")
	protected String studentList(HttpSession session,Model model,String info) {
		model.addAttribute("info",info);
		model.addAttribute("students", studentService.findAll(session));
		return "/admin/student_list";
	}
	@RequestMapping("student/addView.do")
	protected String studentAddView(Model model) {
		model.addAttribute("classes", classService.findAll());
		return "/admin/student_add";
	}
	@RequestMapping("student/add.do")
	protected String studentAdd(Model model,Student student,HttpSession session) {
		String info = studentService.add(student, session);
		model.addAttribute("info", info);
		if(info.equals("添加成功")) {
			return "redirect:/admin/student/list.do";
			
		}else {
			return "forward:/admin/student/addView.do";
		}
	}
	@RequestMapping("student/alterView.do")
	protected String studentAlterView(Model model,HttpSession session,String id) {
		model.addAttribute("student",studentService.findById(id));
		model.addAttribute("classes", classService.findAll());
		return "/admin/student_alter";
	}
	@RequestMapping("student/alter.do")
	protected String studentAlter(Model model,HttpSession session,Student student) {
		String info = studentService.alter(student, session);
		model.addAttribute("info",info);
		if(info.equals("修改成功")) {
			return "redirect:/admin/student/list.do";
		}else {
			model.addAttribute("teacher", student);
			return "forward:/admin/student/addView.do";
		}
	}
	@RequestMapping("student/delete.do")
	protected String studentDelete(Model model,HttpSession session,Student student) {
		String info = studentService.delete(student, session);
		model.addAttribute("info", info);
		if(info.equals("删除成功")) {
			return "redirect:/admin/student/list.do";
		}else {
			return "forward:/admin/student/list.do";
		}
	}
	@RequestMapping("score/list.do")
	protected String scoreList(Model model) {
		model.addAttribute("scores",scoreService.findAll() );
		return "/admin/score_list";
	}
	@RequestMapping("score/alterView.do")
	protected String scoreUpdateView(Model model,Score score) {
		model.addAttribute("score",scoreService.findById(score));
		return "/admin/score_alter";
	}
	@RequestMapping("score/alter.do")
	protected String scoreAlter(Model model,HttpSession session,Score score) {
		String info = scoreService.alter(score, session);
		model.addAttribute("info",info);
		if(info.equals("修改成功")) {
			return "redirect:/admin/score/list.do";
		}else {
			model.addAttribute("id",score.getId());
			return "forward:/admin/score/alterView.do";
		}
	}
	@RequestMapping("score/delete.do")
	protected String scoreDelete(Score score,HttpSession session,Model model) {
		String info = scoreService.delete(score, session);
		model.addAttribute("info",info);
		if(info.equals("删除成功")) {
			return "redirect:/admin/score/list.do";
		}else {
			return "forward:/admin/score/list.do";
		}
	}
	@RequestMapping("subject/list.do")
	protected String subjectList(Model model,String info) {
		model.addAttribute("info",info);
		model.addAttribute("subjects", subjectService.findAll());
		return "/admin/subject_list";
	}
	@RequestMapping("subject/addView.do")
	protected String subjectAddView(Model model) {
		return "/admin/subject_add";
	}
	@RequestMapping("subject/add.do")
	protected String subjectAdd(Model model,HttpSession session,Subject subjcet) {
		String info = subjectService.add(subjcet, session);
		model.addAttribute("info", info);
		if(info.equals("添加成功")) {
			return "redirect:/admin/subject/list.do";
			
		}else {
			return "forward:/admin/subject/addView.do";
		}
	}
	@RequestMapping("subject/alterView.do")
	protected String subjectAlterView(String id,Model model) {
		model.addAttribute("subject",subjectService.findById(id));
		return "/admin/subject_alter";
	}
	@RequestMapping("subject/alter.do")
	protected String subjectAlter(Subject subject,HttpSession session,Model model) {
		String info = subjectService.alter(subject, session);
		System.out.println(subject.getName());
		model.addAttribute("info",info);
		if(info.equals("修改成功")) {
			return "redirect:/admin/subject/list.do";
		}else {
			model.addAttribute("subject", subject);
			return "forward:/admin/subject/addView.do";
		}
	}
	@RequestMapping("subject/delete.do")
	protected String subjectDelete(Subject subject,HttpSession session,Model model) {
		String info = subjectService.delete(subject, session);
		model.addAttribute("info",info);
		if(info.equals("删除成功")) {
			return "redirect:/admin/subject/list.do";
		}else {
			return "forward:/admin/subject/list.do";
		}
		
	}
}
